package emulator.hardware.nmos6502;

import emulator.hardware.HwByte;

/** Byte containing 6502 Flags with access by flag name */
public class Flags6502 extends HwByte
{
	public Flags6502() {
		super();
	}
	public Flags6502(long number) {
		super(number);
	}
	public static final int BIT_CARRY = 0;
	public static final int BIT_ZERO = 1;
	public static final int BIT_INTERRUPT = 2;
	public static final int BIT_DECIMAL = 3;
	public static final int BIT_BREAK = 4;
	public static final int BIT_OVERFLOW = 6;
	public static final int BIT_NEGATIVE = 7;

	public static final int FLAG_CARRY = 1 << BIT_CARRY;
	public static final int FLAG_ZERO = 1 << BIT_ZERO;
	public static final int FLAG_INTERRUPT = 1 << BIT_INTERRUPT;
	public static final int FLAG_DECIMAL = 1 << BIT_DECIMAL;
	public static final int FLAG_BREAK = 1 << BIT_BREAK;
	public static final int FLAG_OVERFLOW = 1 << BIT_OVERFLOW;
	public static final int FLAG_NEGATIVE = 1 << BIT_NEGATIVE;

	public boolean getCarry() { return getBit(BIT_CARRY); }
	public void setCarry(boolean value) { setBit(BIT_CARRY, value); }
	
	public boolean getZero() { return getBit(BIT_ZERO); }
	public void setZero(boolean value) { setBit(BIT_ZERO, value); }
	
	public boolean getInterrupt() { return getBit(BIT_INTERRUPT); }
	public void setInterrupt(boolean value) { setBit(BIT_INTERRUPT, value); }

	public boolean getDecimal() { return getBit(BIT_DECIMAL); }
	public void setDecimal(boolean value) { setBit(BIT_DECIMAL, value); }

	public boolean getBreak() { return getBit(BIT_BREAK); }
	public void setBreak(boolean value) { setBit(BIT_BREAK, value); }

	public boolean getOverflow() { return getBit(BIT_OVERFLOW); }
	public void setOverflow(boolean value) { setBit(BIT_OVERFLOW, value); }

	public boolean getNegative() { return getBit(BIT_NEGATIVE); }
	public void setNegative(boolean value) { setBit(BIT_NEGATIVE, value); }

	public void setNZFromValue(HwByte value) {
		setBit(BIT_ZERO, value.isZero());
		setBit(BIT_NEGATIVE, value.isNegative());
	}
	public void setNZFromValue(int value) {
		setNZFromValue(new HwByte(value));
	}
	
	public String toFlagString() {
		String flagstr = "";
		flagstr += (getNegative() ? "N" : " ");
		flagstr += (getOverflow() ? "V" : " ");
		flagstr += "-";
		flagstr += (getBreak() ? "B" : " ");
		flagstr += (getDecimal() ? "D" : " ");
		flagstr += (getInterrupt() ? "I" : " ");
		flagstr += (getZero() ? "Z" : " ");
		flagstr += (getCarry() ? "C" : " ");
		return flagstr;
	}
	
	public void fromFlagString(String flagstr)
	{
		setNumber(0);
		for (int i = 0; i < flagstr.length(); i++)
		{
			switch (flagstr.charAt(i))
			{
			case 'n':	case 'N':	setNegative(true);	break;
			case 'v':	case 'V':	setOverflow(true);	break;
			case 'b':	case 'B':	setBreak(true);		break;
			case 'd':	case 'D':	setDecimal(true);	break;
			case 'i':	case 'I':	setInterrupt(true);	break;
			case 'z':	case 'Z':	setZero(true);		break;
			case 'c':	case 'C':	setCarry(true);		break;
			}
		}
	}
}
